Allow testing optional dependencies
authorAlex Crichton <alex@alexcrichton.com>
Wed, 21 Oct 2015 00:19:56 +0000 (17:19 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 21 Oct 2015 00:22:18 +0000 (17:22 -0700)
Previously a warning was issued if both -p and --features were passed as flags,
and the rationale for this was that if --features modified the activated set of
features in the package selected by -p it would alter Cargo.lock, which is
undesirable as Cargo.lock should be stable.

This commit, however, interprets --features as changing the resolved graph of
the top-level package, and then -p is a query on that resolved graph. This way
the Cargo.lock file never changes and you're allowed to test optional
dependencies.

src/cargo/ops/cargo_compile.rs
tests/test_cargo_test.rs

index d4c4fef47d159d3b54bb5b06c7c356ad3d09bf6e..a00b86ff912b264016d27330337b2dd10ec25551 100644 (file)
@@ -110,10 +110,6 @@ pub fn compile_pkg<'a>(root_package: &Package,
         s.split(' ')
     }).map(|s| s.to_string()).collect::<Vec<String>>();
 
-    if spec.len() > 0 && (no_default_features || features.len() > 0) {
-        return Err(human("features cannot be modified when the main package \
-                          is not being built"))
-    }
     if jobs == Some(0) {
         return Err(human("jobs must be at least 1"))
     }
index f5ebe9dcc63852687e8673133153c8b25dcc312a..3cfb35cd2bbbf87307a36c1c05deefff35e527db 100644 (file)
@@ -2057,3 +2057,33 @@ test!(selective_test_wonky_profile {
 {running} `rustc src[..]lib.rs [..]`
 ", compiling = COMPILING, running = RUNNING)));
 });
+
+test!(selective_test_optional_dep {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+
+            [dependencies]
+            a = { path = "a", optional = true }
+        "#)
+        .file("src/lib.rs", "")
+        .file("a/Cargo.toml", r#"
+            [package]
+            name = "a"
+            version = "0.0.1"
+            authors = []
+        "#)
+        .file("a/src/lib.rs", "");
+    p.build();
+
+    assert_that(p.cargo("test").arg("-v").arg("--no-run")
+                 .arg("--features").arg("a").arg("-p").arg("a"),
+                execs().with_status(0).with_stdout(&format!("\
+{compiling} a v0.0.1 ([..])
+{running} `rustc a[..]src[..]lib.rs [..]`
+{running} `rustc a[..]src[..]lib.rs [..]`
+", compiling = COMPILING, running = RUNNING)));
+});